//
//  CPDebugConfigViewController.m
//  CPSecurityTools
//
//  Created by collegepre on 2019/11/15.
//  Copyright © 2019 collegepre. All rights reserved.
//

#import "CPDebugConfigViewController.h"
#import <UIKit/UIKit.h>
#import "CPDebugConfigTableViewCell.h"
@interface CPDebugConfigViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *dataList;
@property (nonatomic, strong) NSIndexPath *firstIndexPath;
@property (nonatomic, strong) NSIndexPath *secondIndexPath;

@end

@implementation CPDebugConfigViewController

- (NSArray *)dataList{
    if (!_dataList) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"evi.json" ofType:nil];
        NSData *data = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:path]];
        if (data !=nil) {
            NSArray *arr = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            if (arr) {
                _dataList = arr;
            }
        }else{
            _dataList = [NSArray array];
        }
    }
    return _dataList;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setupViews];
    
    
}

- (void)setupViews{
    self.navigationController.navigationBar.backgroundColor = [UIColor lightGrayColor];
    UIButton *backBtn = [[UIButton alloc] initWithFrame:CGRectMake(-5, 0, 44, 44)];
    [backBtn setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
    backBtn.contentEdgeInsets = UIEdgeInsetsMake(10, -5, 10, 30);
    
    [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    [leftView addSubview:backBtn];

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftView];
    
    UIButton *ensureBtn = [[UIButton alloc] initWithFrame:CGRectMake(-5, 0, 44, 44)];
    [ensureBtn setTitle:@"确定" forState:UIControlStateNormal];
    [ensureBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [ensureBtn addTarget:self action:@selector(ensureBtnClick) forControlEvents:UIControlEventTouchUpInside];
    UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    [rightView addSubview:ensureBtn];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightView];
    
    UITableView *tableView = [[UITableView  alloc] init];
    tableView.allowsMultipleSelection = YES;
    tableView.frame = CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
    [self.view addSubview:tableView];
    self.tableView = tableView;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
}

- (void)ensureBtnClick{
     NSDictionary *dict1 = self.dataList[0];
     NSArray *arr1 = dict1[@"values"];
    NSDictionary *dict11 = arr1[self.firstIndexPath.row];
    NSUserDefaults *UserDef = [NSUserDefaults standardUserDefaults];
    [UserDef setObject:dict11[@"subtitle"] forKey:dict1[@"key"]];
   
    
    NSDictionary *dict2 = self.dataList[1];
     NSArray *arr2 = dict2[@"values"];
    NSDictionary *dict22 = arr2[self.secondIndexPath.row];
     [UserDef setObject:dict22[@"subtitle"] forKey:dict2[@"key"]];
     [UserDef synchronize];
    
    [self back];
}

- (void)back{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataList.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSDictionary *dict = self.dataList[section];
    NSArray *arr = dict[@"values"];
    return arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CPDebugConfigTableViewCell *cell = [[CPDebugConfigTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"UITableViewCell"];
    NSDictionary *dict1 = self.dataList[indexPath.section];
    NSArray *arr = dict1[@"values"];
    NSDictionary *dict = arr[indexPath.row];
    cell.dict = dict;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        if (self.firstIndexPath) {
             CPDebugConfigTableViewCell *cell = [tableView cellForRowAtIndexPath:self.firstIndexPath];
                   cell.selected = NO;
        }
       
        self.firstIndexPath = indexPath;
        CPDebugConfigTableViewCell *cell = [tableView cellForRowAtIndexPath:self.firstIndexPath];
        cell.selected = YES;
    }else if (indexPath.section == 1){
         if (self.secondIndexPath) {
                    CPDebugConfigTableViewCell *cell = [tableView cellForRowAtIndexPath:self.secondIndexPath];
                          cell.selected = NO;
               }
               self.secondIndexPath = indexPath;
               CPDebugConfigTableViewCell *cell = [tableView cellForRowAtIndexPath:self.secondIndexPath];
               cell.selected = YES;
    }
}



- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
     NSDictionary *dict1 = self.dataList[section];
    UIView *header = [[UIView alloc] init];
    header.backgroundColor = [UIColor darkGrayColor];
    header.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 40);
    
    UILabel *label = [[UILabel alloc] init];
    label.textColor = [UIColor whiteColor];
    label.frame = header.bounds;
    label.text = dict1[@"title"];
    [header addSubview:label];
    return header;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 45;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}


@end
